package com.tony.chartdemo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.Utils;
import com.tony.chartdemo.MyMarkerView;
import com.tony.chartdemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tony on 2016/7/19.
 */

public class LineChartLegend extends android.support.v4.app.Fragment {
    private LineChart lineChart;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_linechart_legend, null);
        initView(view);
        initLineChart();
        setData();
        return view;
    }

    private void initView(View view) {
        lineChart = (LineChart) view.findViewById(R.id.lineChart_legend);
        Legend l = lineChart.getLegend();
        l.setPosition(Legend.LegendPosition.ABOVE_CHART_LEFT);


    }

    private void initLineChart() {
        //设置X轴方向的动画
        lineChart.animateX(500);
        lineChart.setPinchZoom(true);
        lineChart.setNoDataTextDescription("暂无数据");
        //添加一个标记，有点像地图的覆盖物
        MyMarkerView mv = new MyMarkerView(getActivity(), R.layout.custom_marker_view);
        lineChart.setMarkerView(mv);

        //实例化一个限制线及其样式
        LimitLine l = new LimitLine(50f,"暴晒");
        l.setLineWidth(Utils.convertDpToPixel(2));
        l.setLineColor(getResources().getColor(android.R.color.holo_red_light));
        l.setTextSize(22);
        l.enableDashedLine(30,10,10);

        XAxis xAxis = lineChart.getXAxis();
        //设置X轴上标签的显示位置
        xAxis.setPosition(XAxis.XAxisPosition.BOTH_SIDED);
        xAxis.setGranularity(1f);
        xAxis.setCenterAxisLabels(true);
        //设置与X轴垂直的表格线是虚线
        xAxis.enableGridDashedLine(50,10,10);

        YAxis leftYAxis = lineChart.getAxisLeft();
        //设置一个限制线
        leftYAxis.addLimitLine(l);
        //设置与Y轴垂直的表格线是虚线
        leftYAxis.enableGridDashedLine(50,10,10);

        YAxis rightYAxis = lineChart.getAxisRight();
        //隐藏Y右轴
        rightYAxis.setEnabled(false);

    }
    //设置数据
    private void setData() {
        LineDataSet set1;
        int[] ints = {32, 45, 67, 65, 76, 54};
        List<Entry> entryList = new ArrayList<>();

        for (int i = 0; i < ints.length; i++) {
            Entry e = new Entry(i+1,ints[i]);
            entryList.add(e);
        }
        if (lineChart.getData() != null && lineChart.getData().getDataSetCount() > 0) {
            set1 = (LineDataSet) lineChart.getData().getDataSetByIndex(0);
            set1.setValues(entryList);
            lineChart.getData().notifyDataChanged();
            lineChart.notifyDataSetChanged();
        } else {
            set1 = new LineDataSet(entryList,"天气情况");
            set1.setColor(getResources().getColor(android.R.color.holo_green_light));
            set1.setLineWidth(2f);
            set1.setDrawCircleHole(true);
            //设置lineChart每个点空心圆的颜色
            set1.setCircleColor(getResources().getColor(android.R.color.holo_red_dark));
            //设置空心圆圆的半径
            set1.setCircleRadius(10f);
            //设置内圆的颜色
            set1.setCircleColorHole(getResources().getColor(android.R.color.background_dark));
            //设置内圆的半径
            set1.setCircleHoleRadius(5f);
            //射线下面的阴影部分
            set1.setDrawFilled(true);
            //设置折线下面阴影部分的图片
//            set1.setFillDrawable(getResources().getDrawable(android.R.drawable.alert_light_frame));


            List<ILineDataSet> lineDataSetList = new ArrayList<>();
            lineDataSetList.add(set1);
            LineData lineData = new LineData(lineDataSetList);
            lineData.setValueTextSize(16);
            lineChart.setData(lineData);
        }


    }
}
